package com.anon_soft.anon_edit;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.text.Editable;
import android.util.Log;

public class CodeEditFragment extends Fragment {
	
	private		String			TAG = "CodeEditFragment";
	private		Uri				m_fUri;
	private		ViewGroup		cont;
	private		CharSequence	text;
	private		int				m_id;
	private		boolean			m_hasBeenEditedSinceLastSave;
	private		ASEditText		m_asedittext;
	
	
	static CodeEditFragment newInstance(int id)
	{
		CodeEditFragment c = new CodeEditFragment();
		Bundle args = new Bundle();
		args.putInt("m_id", id);
		c.setArguments(args);
		
		Log.i("CodeEditFragment", "SFFFF | newInstance | c.getArguments().getInt() = " + c.getArguments().getInt("m_id"));
		
		return c;
	}

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		if ( savedInstanceState != null )
		{
			m_id = savedInstanceState.getInt("m_id");
		}
		else
		{
			Log.i(TAG, "FFFFF | onCreate | savedinstanceState is NULL!" );
		}
		
		Log.i(TAG, "FFFFF | onCreate | m_id = " + m_id);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		
		View v = inflater.inflate(R.layout.codeeditlayout, container, false);
		cont = container;
		m_asedittext = ((ASEditText) v.findViewById(R.id.codeEditText));
		
		if ( text != null )
			m_asedittext.setText(text);
		else
			m_asedittext.setText("");
		
		if ( savedInstanceState != null )
			Log.i(TAG, "FFFFF | onCreateView | savedInstanceState.getInt() = " + savedInstanceState.getInt("m_id"));
		else
			Log.i(TAG, "FFFFF | onCreateView");
		
		return v; 
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		Log.i(TAG, "FFFFF | onDestroy");
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		Log.i(TAG, "FFFFF | onPause");
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		Log.i(TAG, "FFFFF | onResume");
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		Log.i(TAG, "FFFFF | onStop");
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		
		Log.i(TAG, "FFFFF | onSaveInstanceState");
	}
	
	public boolean doesASEditTextNeedToBeSaved()
	{
		return m_asedittext.needToBeSaved();
	}
	
	public boolean	saveASEditText()
	{
		File			f = new File(m_fUri.getPath());
		OutputStream	out = null;
		
		if ( f == null )
		{
			Log.w(TAG, "saveASEditText - f was null!");
			
			return false;
		}
		
		if ( f.canWrite() == false )
		{
			Log.w(TAG, "saveAsEditText - f.canWrite() is false!");
			return false;
		}

		try
		{
			out = new BufferedOutputStream(new FileOutputStream(f));
			
			if ( out != null )
			{
				
				out.write(m_asedittext.getText().toString().getBytes());
				
				out.close();
			}
			else
			{
				Log.w(TAG, "saveAsEditText - out is null!");
			}
			
		}
		catch (IOException e)
		{
			Log.e(TAG, e.toString());
		}
		
		return true;
	}
	
	public boolean	openASEditText()
	{
		File			f = new File(m_fUri.getPath());
		InputStream	in = null;
		
		if ( f == null )
			return false;
		
		if ( f.canRead() == false )
			return false;
		
		try
		{
			in = new BufferedInputStream(new FileInputStream(f));
			
		}
		catch (IOException e)
		{
			Log.e(TAG, e.toString());
		}
		
		return true;
	}
	
	/**
	 * Checks to see if the passed file Uri exists or not
	 * @param fUri The Uri of a file
	 * @return If the passed Uri exists it returns true, otherwise it returns false
	 */
	public boolean	fileUriExists(Uri  fUri)
	{
		File		f;
		boolean		exists;
		
		if ( fUri == null )
			return false;
		
		f = new File(fUri.getPath());
		
		exists = f.exists();
		
		return exists;
	}
	
	public void		setFragmentFileUri(Uri	fUri)
	{
		if ( fUri == null )
			return;
		
		m_fUri = fUri;
	}
	
	
	public Uri	getFragmentFileUri()
	{
		return m_fUri;
	}
}
